﻿Option Explicit On
Option Strict On

Partial Class private_secure_members_manage_MyCampaign
    Inherits System.Web.UI.Page

    Dim u As MembershipUser
    Dim personId As Int32
    Dim delid As Integer
    Dim entityid As Integer
    Protected Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
    End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        u = Membership.GetUser(HttpContext.Current.User.Identity.Name)

        If Not Page.IsPostBack Then

            ' Changed this around
            'If an associate fine
            If User.IsInRole("ListAssociate") Then
                DropDownList1.DataTextField = "Name"
                DropDownList1.DataValueField = "ID"
                DropDownList1.DataSource = MyEntityODS0
                DropDownList1.DataBind()
                DropDownList1.Items.Insert(0, New ListItem("Please Select ...", [String].Empty))
                DropDownList1.SelectedIndex = 0
            Else
                ' If User.IsInRole("ListAgent") Then
                DropDownList1.DataTextField = "Name"
                DropDownList1.DataValueField = "ID"
                DropDownList1.DataSource = MyEntityODS
                DropDownList1.DataBind()
                DropDownList1.Items.Insert(0, New ListItem("Please Select ...", [String].Empty))
                DropDownList1.SelectedIndex = 0
                ' End If
            End If

        End If
        Try
            GridView1.DataBind()
        Catch ex As Exception

        End Try
        'FormView1.DataBind()
        txtDate.Text = DateTime.Now.ToString()
    End Sub


    Protected Sub MyCampaignODS_Selecting(sender As Object, e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) Handles MyEntityODS.Selecting
        e.InputParameters("UserId") = u.ProviderUserKey
    End Sub
    Protected Sub MyCampaignODS0_Selecting(sender As Object, e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) Handles MyEntityODS0.Selecting
        e.InputParameters("UserId") = u.ProviderUserKey
    End Sub

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        ''DetailsView1.ChangeMode(DetailsViewMode.Insert)
        'Dim entityidtextbox As TextBox = DirectCast(DetailsView1.FindControl("TextBox1"), TextBox)
        'Dim datecreatedbox As TextBox = DirectCast(DetailsView1.FindControl("TextBox2"), TextBox)
        'entityidtextbox.Text = DropDownList1.SelectedValue
        'datecreatedbox.Text = CStr(DateTime.Now)
        txtEntityId.Text = DropDownList1.SelectedValue
        txtDate.Text = CStr(DateTime.Now)
        Panel1.Visible = True
        Panel2.Visible = False
        TabContainer1.ActiveTabIndex = 1
        PanelTabHolder.Visible = True
    End Sub

    Protected Sub GridView1_DataBound(sender As Object, e As System.EventArgs) Handles GridView1.DataBound
        'If GridView1.Rows.Count = 0 Then
        '    DetailsView1.ChangeMode(DetailsViewMode.Insert)
        'End If
    End Sub

    'Protected Sub ObjectDataSource4_Selecting(sender As Object, e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) Handles ObjectDataSource4.Selecting
    '    e.InputParameters("Id") = Nothing
    'End Sub

    Protected Sub SqlDataSource2_Inserted(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource2.Inserted
        GridView1.DataBind()
        PanelTabHolder.Visible = False

    End Sub

    Protected Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
        If Not DropDownList1.SelectedValue = Nothing Then
            If Not txtName.Text = "" Then
                txtEntityId.Text = DropDownList1.SelectedValue
                SqlDataSource2.Insert()
                GridView1.DataBind()
                Label2.Text = ""

            Else
                Label2.Text = "Please enter Campaign Name "
            End If

        Else
            Label1.Text = "Please Select A Campaign"
        End If


    End Sub

    Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles GridView1.SelectedIndexChanged
        Dim EntityIdKey As Int32 = CInt(GridView1.SelectedDataKey.Values().Item("EntityId"))
        HiddenField1.Value = CStr(GridView1.SelectedDataKey.Values().Item("Id"))
        HiddenField2.Value = CStr(GridView1.SelectedDataKey.Values().Item("EntityId"))
        Dim selectedid As Integer = CInt(GridView1.SelectedValue)

        Dim usp_UserCampaignSelectTA As New LogginsLogDSTableAdapters.usp_UserCampaignTableAdapter()
        Dim usp_UserCampaignSelectDT As LogginsLogDS.usp_UserCampaignDataTable
        usp_UserCampaignSelectDT = usp_UserCampaignSelectTA.GetData(EntityIdKey)
        Dim campaign = (From ucampaign In usp_UserCampaignSelectDT.AsEnumerable() Where ucampaign.Id = Convert.ToDouble(HiddenField1.Value) Select ucampaign).FirstOrDefault()
        txtEntityIdUpdate.Text = CStr(campaign.EntityId)
        If Not campaign.IsNameNull Then
            txtNameUpdate.Text = campaign.Name
        Else
            txtNameUpdate.Text = String.Empty
        End If
        If Not campaign.IsNoteNull Then
            txtNoteUpdate.Text = campaign.Note
        Else
            txtNoteUpdate.Text = String.Empty
        End If

        ddlMessageType0.SelectedValue = CStr(campaign.MessageTypeId)
        If Not campaign.IsTypeIdNull Then
            DropDownList2.SelectedValue = CStr(campaign.TypeId)
        End If

        'For Each usercampainRow As LogginsLogDS.usp_UserCampaignRow In usp_UserCampaignSelectDT
        'txtEntityIdUpdate.Text = CStr(usercampainRow.EntityId)
        'txtNameUpdate.Text = usercampainRow.Name
        'txtNoteUpdate.Text = usercampainRow.Note
        'DropDownList2.SelectedValue = CStr(usercampainRow.TypeId)
        'txtDateUpdate.Text = CStr(DateTime.Now)
        'Next

        txtDateUpdate.Text = CStr(DateTime.Now)
        TabContainer1.ActiveTabIndex = 0
        Panel2.Visible = True
        PanelTabHolder.Visible = True

    End Sub

    Protected Sub Button3_Click(sender As Object, e As System.EventArgs) Handles Button3.Click
        If Not txtNameUpdate.Text = "" Then

            SqlDataSource3.Update()
            Panel2.Visible = False
            Label3.Text = ""

        Else
            Label3.Text = "Please enter a Campaign Name "
        End If

    End Sub

    Protected Sub Button4_Click(sender As Object, e As System.EventArgs) Handles Button4.Click
        'Panel1.Visible = True
        'Panel2.Visible = False
        PanelTabHolder.Visible = False

    End Sub

    Protected Sub GridView1_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
        Dim delcampaign As New LogginsLogDSTableAdapters.usp_UserCampaignTableAdapter
        delcampaign.Delete(CType(e.Keys(0), Integer?))
        GridView1.DataBind()
        e.Cancel = True
    End Sub

    Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

        If Not DropDownList1.SelectedValue = "" Then
            Panel1.Visible = True
            PanelTabHolder.Visible = True
            TabContainer1.ActiveTabIndex = 1
            Button1.Visible = True
        Else
            'Panel1.Visible = False
            PanelTabHolder.Visible = False
            Button1.Visible = False
        End If
    End Sub

    Protected Sub SqlDataSource3_Updated(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource3.Updated
        PanelTabHolder.Visible = False
        GridView1.DataBind()
    End Sub

    Protected Sub Button5_Click(sender As Object, e As System.EventArgs)
        'Nenad: This needs to be coded
    End Sub

    Protected Sub GridView1_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "NewCampaign" Then
            txtEntityId.Text = DropDownList1.SelectedValue
            txtDate.Text = CStr(DateTime.Now)
            Panel1.Visible = True
            Panel2.Visible = False
            TabContainer1.ActiveTabIndex = 1
            PanelTabHolder.Visible = True
        End If
    End Sub
End Class
